package cn.edu.nuc.film.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Function;
import cn.edu.nuc.film.entity.Role;
import cn.edu.nuc.film.service.interfaces.RoleService;

@Controller
@RequestMapping(value="/film/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	
	/**
	 * 角色列表
	 * @return
	 */
	@RequestMapping(value="/rolelist",method=RequestMethod.GET)
	public String rolelist(String sname,HttpServletRequest request){
		
		//获取要查询的名字
		//String sname = request.getParameter("sname");
		if( sname == null || sname.equals("")){
			sname = null;
		}
		//设置分页信息
		PageControl pc = new PageControl();
		
		//设置初始当前页currindex = 1
		Integer currindex = 1;
		
		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if(request.getParameter("index")!=null){
			currindex = Integer.parseInt(request.getParameter("index"));
		}
		
		//将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
		
		//调用service中的分页方法，获取分页信息
		Pager<Role> pager = roleService.rolelist(pc, sname);
		//将分页信息传送到前台
		request.setAttribute("pager", pager);
		return "sys/role/list";
	}
	
	/**
	 * 进入添加角色界面
	 * @return
	 */
	@RequestMapping(value="/toinsertRole",method=RequestMethod.GET)
	public String toinsertRole(){
		return "sys/role/add";
	}
	
	/**
	 * 执行添加操作
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/insertRole",method=RequestMethod.POST)
	public String insertRole( Role role){
		/*int count = */
		roleService.insert(role);
		return "redirect:rolelist";
	}
	
	/**
	 * 获取role的详细信息
	 * @param roleid
	 * @param model
	 * @return   
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public String detail( int roleid,Model model){
		
		Role role = roleService.detail(roleid);
		model.addAttribute("role", role);
		return "sys/role/detail";
		
	}
	
	/**
	 * 进入编辑界面
	 * @param roleid
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toeditRole",method=RequestMethod.GET)
	public String toeditRole( int roleid,Model model){
		Role role = roleService.detail(roleid);
		model.addAttribute("role", role);
		return "sys/role/edit";
	}
	
	/**
	 * 执行编辑操作
	 * @param role(role对象作为传值参数)
	 * @return
	 */
	@RequestMapping(value="/editRole",method=RequestMethod.POST)
	public String editRole( Role role){
		
		/*int count = */
		roleService.updateRole(role);
		return "redirect:rolelist";
	}
	
	/**
	 * 删除角色，通过roleid来进行删除
	 * @return
	 */
	@RequestMapping(value="/deleteRole",method=RequestMethod.GET)
	public String deleteRole( int roleid){
		/*int count = */
		roleService.deleteRole(roleid);
		return "redirect:rolelist";
	}
	
	@RequestMapping(value="/initFunction",method=RequestMethod.GET)
	public String initFunction( int roleid,Model model){
		
		//调用获取角色详细信息的方法（主要用来显示rolename以及通过roleid来进行编辑role的功能）
		Role role = roleService.detail(roleid);
		//获取角色的权限
		List<Function> list = roleService.initfunction(role);
		//将获取的角色功能存放到list集合里进行提交到jsp界面
		model.addAttribute("list", list);
		//返回role的信息
		model.addAttribute("role", role);
		return "sys/role/right";
	}
	
	/**
	 * 保存角色权限
	 * @param roleid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/saveright",method=RequestMethod.POST)
	public String saveright( int roleid, HttpServletRequest request){
		
		String[] funids = request.getParameterValues("ckrr");
		System.out.println(funids);  
		//删除角色权限
		roleService.deleteRight(roleid);
		//执行
		int count1 = roleService.saveright(roleid,funids);
		if( count1 > 0){
			return "redirect:rolelist";
		}else{
			request.setAttribute("msg", "保存角色权限失败");
			return "sys/role/right";
		}
		
	}
	
}
